package controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import bizlogic.OverTimeController;

import model.EmployeeDTO;
import model.OvertimeDTO;
import java.text.*;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
 * Servlet implementation class ClaimCompServelet
 */
@WebServlet("/ClaimCompServelet")
public class ClaimCompServelet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ClaimCompServelet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		RequestDispatcher rd = null;
		OvertimeDTO ovtDTO = new OvertimeDTO();
		OverTimeController ovtManager = new OverTimeController();
		String forwardTo ="";
		Date d = new Date();
		
		HttpSession session = request.getSession();
		EmployeeDTO user = (EmployeeDTO) session.getAttribute("user");
			
		Double duration = Double.parseDouble(request.getParameter("totalHours"));
		if(duration > 0 && duration < 8){
			ovtDTO.setTotalHours(duration);
		}
		else forwardTo = "/claimcompformJSP.jsp";
		
		ovtDTO.setEmployeeID(user.getEmployeeID()); // session
		ovtDTO.setStatus("Applied");
		ovtDTO.setCreateBy(user.getEmployeeID());
		
		
		
		java.sql.Date currentdate = new java.sql.Date(new Date().getTime());// current date yyyy-mm-dd
		ovtDTO.setCreateDate(currentdate);
		
		ovtDTO.setDescription(request.getParameter("description"));
		String str_date = request.getParameter("date");
		Date ovtdate = java.sql.Date.valueOf(str_date);
		if (ovtdate.after(d))
			forwardTo = "/claimcompformJSP.jsp";
		else
			forwardTo = "/ConfirmClaimComp.jsp";
		
		ovtDTO.setDate((java.sql.Date) ovtdate);			
		ovtDTO.setModifiedDate(currentdate);
		
	    ovtManager.AddCompensation(ovtDTO);
	    
	    rd = request.getRequestDispatcher(forwardTo);
		rd.forward(request, response);
		
		
		
			
		
	}

}
